<?php

namespace app\index\validate;

use think\Validate;
use think\facade\Cache;

class User extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */
	protected $rule = [
        'name' => 'require|length:2,20|token',
        'mobile' => 'require|length:11|unique:user',
        'sms_code' => 'require|length:6,10|checkCode',
        'password' => 'require|length:6,20',
        'password_confirmation' => 'require|length:6,20|confirm:password',
        'captcha' => 'require|captcha',
        'introduction' => 'max:100',
    ];

    protected $message = [
        'name.require' => '用户名不能为空',
        'name.length' => '用户名长度必须在2-20个字符之间',
        'mobile.require' => '手机号码不能为空',
        'mobile.length' => '手机号码格式不正确',
        'mobile.unique' => '当前手机号码已注册',
        'sms_code.require' => '短信验证码不正确',
        'sms_code.length' => '短信验证码不正确',
        'password.require' => '登录密码不能为空',
        'password.length' => '登录密码长度必须在6-20之间',
        'password_confirmation.require' => '重复密码不能为空',
        'password_confirmation.length' => '重复密码长度必须在6-20之间',
        'password_confirmation.confirm' => '两次输入的密码不一致',
        'introduction.max' => '个人介绍信息不能超过100个字符',

    ];

    /**
     * 自定义验证方法-验证用户输入的短信验证码是否正确
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-16
     * @param    string             $value 字段值
     * @param    string             $rule  字段值验证值
     * @param    array              $data  表单提交的所有数据
     * @return   string/true               验证结果
     */
    public function checkCode($value, $rule, $data = [])
    {
        $invalid_msg = '短信验证码不正确';

        if(!isset($data['mobile'])){
            return $invalid_msg;
        }

        $mobile = $data['mobile'];
        $cache_code = Cache::store('redis')->get($mobile);
        if(empty($cache_code)){
            return $invalid_msg;
        }else if($value != $cache_code){
            return $invalid_msg;
        }
        return true;
    }

    protected $scene = [
        'register'  =>  ['name','mobile', 'sms_code', 'password', 'password_confirmation', 'captcha'],
        'reset_password'  =>  ['sms_code', 'password', 'password_confirmation'],
        'update'  =>  ['name', 'introduction'],
    ];
}
